<template>
  <div class="info-content" style="width: 1000px;text-align: center">
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-form-item label="数据源:" prop="ds">
        <el-select
          v-model="form.ds"
          placeholder="请选择数据源"
          filterable
        >
          <el-option
            v-for="item in dataSourceList"
            :key="item.name"
            :label="item.title"
            :value="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称:" prop="name">
        <el-input v-model.trim="form.name" clearable placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="分组:" prop="groupId">
        <el-select v-model="form.groupId" placeholder="请选择分组">
          <el-option
            v-for="item in apiGroup"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="模式:" prop="type" style="display: none">
        <el-radio-group
          v-model="form.type"
          @change="$emit('changeMode', form.type)"
          :disabled="isUpdate"
        >
          <el-radio label="1">开发者模式</el-radio>
          <el-radio label="2">简单模式</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-row :gutter="5" style="display: flex">
        <el-col :span="16">
          <el-form-item label="路径:" prop="path">
            <el-input v-model="API_CONFIG_URL" disabled></el-input>
            <el-input
              placeholder="请输入路径"
              v-model.trim="form.path"
              :disabled="isUpdate"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import website  from '@/framework-core/config/website';
import curdMixin from '@/framework-core/mixins/crud';
import {getCore, listCore} from "@framework-core/api/core/api";
export default {
  mixins: [curdMixin],
  props: {
    value: {
      default: () => {},
      type: Object
    },
    isUpdate: Boolean
  },
  data() {
    const validatePath = (rule, value, callback) => {
      if (value) {
        if (/^[a-zA-Z][\w]*$/.test(value)) {
          callback();
        } else {
          callback('请输入正确路径');
        }
      } else {
        callback('请输入路径');
      }
    };
    return {
      form: {
      },
      rules: {
        ds: [
          {
            required: true,
            message: '请选择数据源',
            trigger: 'change'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }
        ],
        groupId: [
          {
            required: true,
            message: '请选择分组',
            trigger: 'change'
          }
        ],
        path: [
          {
            required: true,
            validator: validatePath,
            trigger: 'blur'
          }
        ]
      },
      API_CONFIG_URL:  website.baseUrl+'/',
      apiGroup: [],
      dataSourceList: []
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    if (!this.form) {
      this.form = {};
    }
    if (this.$route.query.id) {
      getCore("/apis/apisConfig", this.$route.query.id, {}).then(response => {
        this.form = response.data.data.data;
      });
    }

    this.getApisGroup();
    this.getDatabaseList();
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {},
  watch: {
    value: {
      handler(val) {
        if (val && Object.keys(val).length) {
          this.form = val;
        }
      },
      immediate: true
    }
  },
  methods: {
    validate(callback) {
      this.$refs.form.validate((valid) => {
        console.log("valid:"+valid)
        if (!valid) {
          return false;
        }
        callback(this.form);
        this.$emit('update:data', this.form);
      });
    },
    getApisGroup() {
      listCore("/apis/apisGroup",{}).then(response => {
        this.apiGroup = response.data.data.data;
      });

    },
    getDatabaseList() {
      listCore("sys/datasource",{}).then(response => {
        if(!response.data.data){
          this.dataSourceList = [];
          return
        }
        this.dataSourceList = response.data.data.data;
      });

    },
    clearValidate() {
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    }
  }
};
</script>
<style scoped lang="scss">
.el-form-item__content {
  .el-select {
    width: 100%;
  }
}

.info-content {
  margin: 0 auto;
}

/* @import url(); 引入css类 */
</style>
